# Create your views here.
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.core.paginator import Paginator
from myhomework.account.daos import *
from myhomework.account.forms import *
from myhomework.homework.daos import *
from myhomework.homework.forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import authenticate, login
from myhomework.exceptions.exception import *
from myhomework.account.decorators import *
from myhomework.common.decorators import *
from myhomework.common import *
from django.template import RequestContext
from myhomework.filemanager.filemanager import FileManager
import settings
class HomeworkView(object):
    
    @staticmethod
    @process_exception
    def create_homework(request,context={}):
        if request.method == 'POST':          
            form = CreateHomeworkForm(request.POST)
            if form.is_valid():
                homework = form.save(commit=False)
                HomeworkDao.add_homework(homework)
                print 'id!!!!!!!!!!!'
                print homework.id
                return HttpResponse('{"homework":"%d"}'%(homework.id,))
            else:
                raise UserDataFormError(form.errors)
        
        return render_to_response('ajax/create_homework.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @process_exception
    def homework_list(request,context={}):
        return render_to_response('ajax/homework_list.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @process_exception
    def homework_list_content(request,context={}):
        start_s = request.GET.get('start')
        limit_s = request.GET.get('limit')
        
        try:
            start = int(start_s)
        except:
            start = 0
        try:
            limit = int(limit_s)
        except:
            limit = 10
            
        type = request.GET.get('type')
        if(type=='unoverdue'):
            homeworks = HomeworkDao.get_unoverdue_homework_list(start=start,limit=limit)
        elif(type=='overdue'):
            homeworks = HomeworkDao.get_overdue_homework_list(start=start,limit=limit)
        else:
            homeworks = HomeworkDao.get_homework_list(start=start,limit=limit)
        context.update({'homeworks':homeworks})
        #res=render_to_response('ajax/homework_list_content.html',context, context_instance=RequestContext(request))
        #print res.__class__
        return render_to_response('ajax/homework_list_content.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @process_exception
    def homework_count(request,context={}):
        if(type=='unoverdue'):
            count = HomeworkDao.get_unoverdue_homework_count()
        elif(type=='overdue'):
            count = HomeworkDao.get_overdue_homework_count()
        else:
            count = HomeworkDao.get_homework_count() 
            #count = HomeworkDao.get_number_of_user()
        return HttpResponse('{"count":"%d"}'%(count,), mimetype="text/plain")
    
    @staticmethod
    def show_homework(request,context={}):
        id = request.GET.get('homework')
        print id
        homework = HomeworkDao.get_homework_by_id(id)
        
        
        
        context.update({'homework':homework})
        return render_to_response('ajax/show_homework.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    @process_exception
    def assess_homework(request,context={}):
        if request.method == 'POST':          
            form = CreateHomeworkForm(request.POST)
            if form.is_valid():
                homework = form.save(commit=False)
                HomeworkDao.add_homework(homework)
                return HttpResponse('{}')
            else:
                raise UserDataFormError(form.errors)
        id = request.GET.get('id')
        homework = HomeworkDao.get_homework_by_id(id)
        context.update({'homework':homework})
        return render_to_response('ajax/assess_homework.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    #@process_exception
    def submit_homework(request,context={}):
        
        if request.method == 'POST':          
            form = SubmitHomeworkForm(request.POST,request.FILES)
            
            if form.is_valid():
                cd = form.cleaned_data
        
                try:
                    
                    path = FileManager.get_ready_for_saving_homework(int(cd['homework_id']),request.POST['user_id'])
                    handle_uploaded_file(request.FILES['file'],path[0])
                    FileManager.save_homework_submitted(int(cd['homework_id']),request.POST['user_id'])
                except Exception,e:
                    print e
                print SettingDao.get_setting('root_path_of_homework')
                url = '%s%s'%(SettingDao.get_setting('url_of_homework'),path[1].replace('pic.png','index.html').replace(SettingDao.get_setting('root_path_of_homework'),''),)
                pic = '%s%s'%(SettingDao.get_setting('url_of_homework'),path[1].replace(SettingDao.get_setting('root_path_of_homework'),''),)
                src = '%s%s'%(SettingDao.get_setting('url_of_src_file'),path[0].replace(SettingDao.get_setting('root_path_of_src_file'),''),)
                print url
                print pic
                print src
                print path[1]
                capture_page('http://www.baidu.com',path[1])
                return HttpResponse(r'''{"src":"%s","pic":"%s","url":"%s"}'''%(src,pic,url))
            else:
                raise UserDataFormError(form.errors)
            
        id = request.GET.get('homework')
        homework = HomeworkDao.get_homework_by_id(id)
        context.update({'homework':homework})
        return render_to_response('ajax/submit_homework.html',context, context_instance=RequestContext(request))
    
        
    @staticmethod
    #@process_exception
    def show_submited_homework(request,context={}):
        
        #context.update({'homework':homework})
        id = request.GET.get('homework')
        homework = HomeworkDao.get_homework_by_id(id)
        context.update({'homework':homework})
    
        homeworks_submited = HomeworkSubmitedDao.get_homework_submited_list(id,request.user.username)
        print homeworks_submited
        context.update({'homeworks_submited':homeworks_submited})
        return render_to_response('ajax/show_submited_homework.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    #@process_exception
    def my_homework_list(request,context={}):
        
        #context.update({'homework':homework})
        return render_to_response('ajax/my_homework_list.html',context, context_instance=RequestContext(request))
    @staticmethod
    #@process_exception
    def my_homework_list_content(request,context={}):
        
        #context.update({'homework':homework})
        return render_to_response('ajax/my_homework_list_content.html',context, context_instance=RequestContext(request))
    
    @staticmethod
    #@process_exception
    def my_homework_count(request,context={}):
        
        #context.update({'homework':homework})
        return render_to_response('ajax/show_submited_homework.html',context, context_instance=RequestContext(request))
    